import NoticeboardModel from '../model/noticeboardModel.js';
import Result from '../common/result.js';
import { getTokenInfo } from '../common/auth.js';

const NoticeboardController = {
    // Get post list
    async getList(req, res) {
        try {
            if (!req.params.page || isNaN(req.params.page)) {
                req.params.page = 1;
            }
            const studentId = getTokenInfo(req.headers.authorization).id;
            req.query.studentId = studentId;

            const posts = await NoticeboardModel.getList(req.query, req.params.page);
            return Result.success(res, posts);
        } catch (error) {
            console.error('Get post list error:', error);
            return Result.error(res, 'Failed to get post list');
        }
    },

    // Get post details
    async getById(req, res) {
        try {
            const { id } = req.params;
            const studentId = getTokenInfo(req.headers.authorization).id;
            const post = await NoticeboardModel.getById(id, studentId);
            
            if (!post) {
                return Result.error(res, 'Post not found', 404);
            }
            
            return Result.success(res, post);
        } catch (error) {
            console.error('Get post detail error:', error);
            return Result.error(res, 'Failed to get post detail');
        }
    },

    // Create post
    async create(req, res) {
        try {
            const studentId = getTokenInfo(req.headers.authorization).id;
            console.log(req.body);
            console.log(studentId);
            const post = {
                ...req.body,
                studentId
            };
            
            await NoticeboardModel.create(post);
            return Result.success(res, null, 'Post created successfully');
        } catch (error) {
            console.error('Create post error:', error);
            return Result.error(res, 'Failed to create post');
        }
    },

    // Update post
    async update(req, res) {
        try {
            const { id } = req.params;
            const post = await NoticeboardModel.getById(id);
            
            if (!post) {
                return Result.error(res, 'Post not found', 404);
            }
            
            const studentId = getTokenInfo(req.headers.authorization).id;
            if (post.StudentID !== studentId) {
                return Result.error(res, 'No permission to update this post', 403);
            }
            
            await NoticeboardModel.update(id, req.body);
            return Result.success(res, null, 'Post updated successfully');
        } catch (error) {
            console.error('Update post error:', error);
            return Result.error(res, 'Failed to update post');
        }
    },

    // Delete post
    async delete(req, res) {
        try {
            const { id } = req.params;
            const post = await NoticeboardModel.getById(id);
            
            if (!post) {
                return Result.error(res, 'Post not found', 404);
            }
            
            const studentId = getTokenInfo(req.headers.authorization).id;
            if (post.StudentID !== studentId) {
                return Result.error(res, 'No permission to delete this post', 403);
            }
            
            await NoticeboardModel.delete(id);
            return Result.success(res, null, 'Post deleted successfully');
        } catch (error) {
            console.error('Delete post error:', error);
            return Result.error(res, 'Failed to delete post');
        }
    },

    // Toggle like/unlike
    async like(req, res) {
        try {
            const { id } = req.params;
            const studentId = getTokenInfo(req.headers.authorization).id;
            
            const isLiked = await NoticeboardModel.toggleLike(id, studentId);
            return Result.success(res, { isLiked });
        } catch (error) {
            console.error('Toggle like error:', error);
            return Result.error(res, 'Failed to toggle like');
        }
    },

    // Get comment list
    async getComments(req, res) {
        try {
            const comments = await NoticeboardModel.getComments(req.params.id);
            console.log("comments", comments);
            return Result.success(res, comments);
        } catch (error) {
            return Result.error(res, error.message);
        }
    },

    // Add comment
    async addComment(req, res) {
        try {
            if (!req.body.content) {
                return Result.error(res, 'Comment content is required');
            }
            const studentId = getTokenInfo(req.headers.authorization).id;
            const comment = await NoticeboardModel.addComment(
                req.params.id,
                req.body.content,
                studentId
            );
            return Result.success(res, comment);
        } catch (error) {
            return Result.error(res, error.message);
        }
    },

    // Delete comment
    async deleteComment(req, res) {
        try {
            const studentId = getTokenInfo(req.headers.authorization).id;
            await NoticeboardModel.deleteComment(req.params.commentId, studentId);
            return Result.success(res, null);
        } catch (error) {
            return Result.error(res, error.message);
        }
    }
};

export default NoticeboardController;